package edu.rmit.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import edu.rmit.contant.TableConstant;
import edu.rmit.dao.IMessageDAO;
import edu.rmit.domain.Message;
import edu.rmit.util.DBManager;

/**
 * This class implement the message operation. Like add one message, delete one
 * message, and get one group's message.
 * 
 * @author Joey George
 * @version 1.0
 */
public class MessageDAOImpl implements IMessageDAO {

	/**
	 * The database connection management class.
	 */
	private DBManager dbManager = null;

	/**
	 * The constructor without parameter to initialize a database connection.
	 */
	public MessageDAOImpl() {
		dbManager = new DBManager();
	}

	/**
	 * This method implement to insert an message.
	 * 
	 * @param groupId
	 *            The group id which this message belongs to.
	 * @param content
	 *            The content of this message.
	 * @param userName
	 *            The user name who want to add a message.
	 */
	@Override
	public void addMessage(int groupId, String content, String userName) {
		String sqlString = "insert into " + TableConstant.MESSAGE_TABLE
				+ "(id,groupid,creator,content,isactive,createtime)"
				+ " values(" +
				TableConstant.SEQ_MESSAGE + ",?,?,?,?,?)";
		Connection connection = dbManager.getConnection();
		PreparedStatement preparedState = null;
		try {
			preparedState = connection.prepareStatement(sqlString);
			preparedState.setInt(1, groupId);
			preparedState.setString(2, userName);
			preparedState.setString(3, content);
			preparedState.setBoolean(4, true);
			preparedState.setTimestamp(5, new Timestamp(new Date().getTime()));
			
			preparedState.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbManager.closeAll(null, preparedState, connection);
		}
	}

	/**
	 * This method implement to delete an message.
	 * 
	 * @param messageId
	 *            The message id which you want to delete.
	 */
	@Override
	public void deleteMessage(int messageId) {
		String deleteMessageSQL = "update " + TableConstant.MESSAGE_TABLE
				+ " set isactive = ? where id = " + messageId;
		Connection connection = dbManager.getConnection();
		PreparedStatement preparedState = null;
		try {
			preparedState = connection.prepareStatement(deleteMessageSQL);
			preparedState.setBoolean(1, false);
			preparedState.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbManager.closeAll(null, preparedState, connection);
		}
	}

	/**
	 * This method implement to get all the message which belongs to a
	 * particular group.
	 * 
	 * @param groupId
	 *            The group id which contains all the information.
	 * @return The messages which belongs to this group.
	 */
	@Override
	public List<Message> getMessages(int groupId) {
		String sqlString = "select * from " + TableConstant.MESSAGE_TABLE + 
				 " where groupid = ? and isactive = ?";
		Connection connection = dbManager.getConnection();
		List<Message> messages = new ArrayList<Message>();
		try {
			PreparedStatement preparedState = connection.prepareStatement(sqlString);
			preparedState.setInt(1, groupId);
			preparedState.setBoolean(2, true);
			ResultSet rs = preparedState.executeQuery();
			while (rs.next()) {
				Message message = new Message();

				message.setId(rs.getInt("id"));
				message.setActive(rs.getBoolean("isactive"));
				message.setContent(rs.getString("content"));
				message.setGroupId(rs.getInt("groupid"));
				message.setSpeakerName(rs.getString("creator"));
				Calendar date = Calendar.getInstance();
				date.setTimeInMillis(rs.getTimestamp("createtime").getTime());
				message.setCreateTime(date);
				messages.add(message);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return messages;
	}

}
